package com.ruoyi.system.mqtt;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.vo.RealTimeDataVO;
import com.ruoyi.system.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class MqttClientSelfDefine implements MqttCallbackExtended {

    private RedisCache redisCache;

    public MqttClientSelfDefine(RedisCache redisCache) {
        this.redisCache = redisCache;
        log.info("MqttClientSelfDefine init");
    }
    @Override
    public void connectComplete(boolean reconnect, String serverURI) {
        log.info("Successfully connected to " + serverURI + " with reconnection status: " + reconnect);
    }

    @Override
    public void connectionLost(Throwable throwable) {
        log.info("Connection lost");
        throwable.printStackTrace();
    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        log.info("Message arrived on topic: " + topic);
        log.info("Payload: " + new String(message.getPayload()));
        JSONObject jsonObject = JSON.parseObject(new String(message.getPayload()));
        JSONObject rawData = jsonObject.getJSONObject("device01");
        String realTimeKey = "agriculture:iot:device01:real_time";
        redisCache.setCacheObject(realTimeKey, rawData);
        log.info("Message saved to Redis with key: " );
        Double temperatureExpected = rawData.getDouble("temperatureExpected");
        Double temperatureActual = rawData.getDouble("temperatureActual");
        Double humidity = rawData.getDouble("humidity");
        Double soilMoisture = rawData.getDouble("soil_moisture");
        Double soilPH = rawData.getDouble("soil_ph");
        Double soilConductivity = rawData.getDouble("soil_conductivity");
        Double lightIntensity = rawData.getDouble("light_intensity");
        Double co2 = rawData.getDouble("co2");
        RealTimeDataVO data = new RealTimeDataVO();
        String currentTime = rawData.getString("currentTime");
        currentTime = currentTime.substring(11);
        data.setTemperatureExpected(temperatureExpected);
        data.setTemperatureActual(temperatureActual);
        data.setCurrentTime(currentTime);
        // 相对湿度范围 / 湿度范围 30-80%
        data.setHumidity(humidity);
        // 土壤湿度范围 /土壤水分含量范围 0-1
        data.setSoilMoisture(soilMoisture);
        // 常见农作物适宜pH范围 /土壤pH值范围 0-14
        data.setSoilPH(soilPH);
        // 电导率范围 /土壤电导率范围 0-2 mS/cm
        data.setSoilConductivity(soilConductivity);
        // 光照强度范围 /光照强度范围 0-1000 lux
        data.setLightIntensity(lightIntensity);
        // CO2浓度范围
        data.setCo2(co2);
        // 发送到WebSocket
        WebSocketServer.sendMessageToUser("1",JSON.toJSONString(data));
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        log.info("Delivery complete");
    }
}
